<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AjaxChatUtil
 *
 * @author owner
 */
class AjaxChatUtil {

    var $db;
	var $log;
	var $_config;

	function AjaxChatUtil() {
		$this->initialize();
	}

	function initialize() {
		// Initialize configuration settings:
		$this->initConfig();

		// Initialize the DataBase connection:
		$this->initDataBaseConnection();
    }

	function initConfig() {
		$config = null;
		if (!include(AJAX_CHAT_PATH.'lib/config.php')) {
			echo('<strong>Error:</strong> Could not find a config.php file in "'.AJAX_CHAT_PATH.'"lib/". Check to make sure the file exists.');
			die();
		}
		$this->_config = &$config;

	}

	function initDataBaseConnection() {
		// Create a new database object:
		$this->db = new AJAXChatDataBase(
			$this->_config['dbConnection']
		);
		// Use a new database connection if no existing is given:
		if(!$this->_config['dbConnection']['link']) {
			// Connect to the database server:
			$this->db->connect($this->_config['dbConnection']);
			if($this->db->error()) {
				echo $this->db->getError();
				die();
			}
			// Select the database:
			$this->db->select($this->_config['dbConnection']['name']);
			if($this->db->error()) {
				echo $this->db->getError();
				die();
			}
		}
		// Unset the dbConnection array for safety purposes:
		unset($this->_config['dbConnection']);
	}

    function getMessages($query) {

			$sql = 'select * from
						ajax_chat_messages
					WHERE
						text like \'%'.$query.'%\';';

			// Create a new SQL query:
			$result = $this->db->sqlQuery($sql);

			// Stop if an error occurs:
			if($result->error()) {
				echo $result->getError();
				die();
			}

            $messages = array();

			while($row = $result->fetch()) {
				array_push($messages, $row);
			}

		return $messages;
   }
}
?>
